//
//  NSTimeView.m
//  Meboo
//
//  Created by kyo on 2024/5/25.
//

#import "NSTimeView.h"
@interface NSTimeView ()
@property (nonatomic, strong) UIButton *button1;
@property (nonatomic, strong) UIButton *button2;
@property (nonatomic, strong) UIButton *button3;
@property (nonatomic, strong) UIButton *button4;
@property (nonatomic, strong) UIButton *button5;
@property (nonatomic, strong) UIButton *button6;

@property (nonatomic, strong) UIImageView *hmImageView;
@property (nonatomic, strong) UIImageView *msImageView;
@end

@implementation NSTimeView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setSecond:(NSInteger)second {
    _second = second;
}

//- (void)getSecondToday {
//    // 获取当前日期和时间
//    NSDate *currentDate = [NSDate date];
//     
//    // 创建一个日历对象
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//     
//    // 创建一个时间组件对象，设置今天晚上6点的时间
//    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
//    dateComponents.hour = 23; // 晚上6点
//    dateComponents.minute = 59; // 分钟
//    dateComponents.second = 59; // 秒
//     
//    // 将时间组件转换为日期对象
//    NSDate *endOfDayDate = [calendar dateBySettingHour:dateComponents.hour minute:dateComponents.minute second:dateComponents.second ofDate:currentDate options:0];
//     
//    // 计算两个日期之间的差异
//    NSDateComponents *timeUntilSix = [calendar components:NSCalendarUnitSecond fromDate:currentDate toDate:endOfDayDate options:0];
//
//    self.second = timeUntilSix.second+1;
////    return timeUntilSix.second+1;
//}

#pragma mark - Action
- (void)startCount {
//    [self getSecondToday];
    
    
    NSString *hourStr = [NSString stringWithFormat:@"%ld",self.second/3600];
    if (hourStr.length < 2) {
        [self.button1 setTitle:@"0" forState:0];
        [self.button2 setTitle:hourStr forState:0];
    } else {
        [self.button1 setTitle:[hourStr substringToIndex:1] forState:0];
        [self.button2 setTitle:[hourStr substringFromIndex:1] forState:0];
    }
    
    NSString *minStr = [NSString stringWithFormat:@"%ld",self.second%3600/60];
    if (minStr.length < 2) {
        [self.button3 setTitle:@"0" forState:0];
        [self.button4 setTitle:minStr forState:0];
    } else {
        [self.button3 setTitle:[minStr substringToIndex:1] forState:0];
        [self.button4 setTitle:[minStr substringFromIndex:1] forState:0];
    }
    
    NSString *senStr = [NSString stringWithFormat:@"%ld",self.second%3600%60];
    if (senStr.length < 2) {
        [self.button5 setTitle:@"0" forState:0];
        [self.button6 setTitle:senStr forState:0];
    } else {
        [self.button5 setTitle:[senStr substringToIndex:1] forState:0];
        [self.button6 setTitle:[senStr substringFromIndex:1] forState:0];
    }
    
    
    self.second--;
    if (self.second == 0) {
        if (self.timeBlock) {
            self.timeBlock();
        }
    } else {
        [NSTimer scheduledTimerWithTimeInterval:1
                                             target:self
                                           selector:@selector(progressChanged:)
                                           userInfo:nil
                                            repeats:YES];
    }
}

-(void)progressChanged:(NSTimer *)timer {
    
    if (self.second == 0) {
        if (self.timeBlock) {
            self.timeBlock();
        }
        [timer invalidate];
    }
    
    NSString *hourStr = [NSString stringWithFormat:@"%ld",self.second/3600];
    if (hourStr.length < 2) {
        [self.button1 setTitle:@"0" forState:0];
        [self.button2 setTitle:hourStr forState:0];
    } else {
        [self.button1 setTitle:[hourStr substringToIndex:1] forState:0];
        [self.button2 setTitle:[hourStr substringFromIndex:1] forState:0];
    }
    
    NSString *minStr = [NSString stringWithFormat:@"%ld",self.second%3600/60];
    if (minStr.length < 2) {
        [self.button3 setTitle:@"0" forState:0];
        [self.button4 setTitle:minStr forState:0];
    } else {
        [self.button3 setTitle:[minStr substringToIndex:1] forState:0];
        [self.button4 setTitle:[minStr substringFromIndex:1] forState:0];
    }
    
    NSString *senStr = [NSString stringWithFormat:@"%ld",self.second%3600%60];
    if (senStr.length < 2) {
        [self.button5 setTitle:@"0" forState:0];
        [self.button6 setTitle:senStr forState:0];
    } else {
        [self.button5 setTitle:[senStr substringToIndex:1] forState:0];
        [self.button6 setTitle:[senStr substringFromIndex:1] forState:0];
    }
    
    
    self.second--;
    if (self.second == 0) {
        if (self.timeBlock) {
            self.timeBlock();
        }
        [timer invalidate];
    }
}

#pragma mark - UI
- (void)setupUI {
    // 今日剩余毫秒
    // 使用场景，UI组件库倒计时组件，今日剩余时间会使用到
//    NSDate *date = [NSDate date];
//    [date ]
//    Date().setHours(24, 0, 0, 0) - Date.now()
    
    self.backgroundColor = UIColor.clearColor;
    
    CGFloat wid = (ScreenWidth-MBWidthScale(84)-MBWidthScale(47))/6;

    [self addSubview:self.button1];
    [self.button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.centerY.equalTo(self);
        make.width.mas_equalTo(wid);
        make.height.mas_equalTo(MBHeightScale(50));
    }];
    
    [self addSubview:self.button2];
    [self.button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(wid+MBWidthScale(5));
        make.centerY.equalTo(self);
        make.width.mas_equalTo(wid);
        make.height.mas_equalTo(MBHeightScale(50));
    }];
    
    [self addSubview:self.button3];
    [self.button3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(wid*2+MBWidthScale(21));
        make.centerY.equalTo(self);
        make.width.mas_equalTo(wid);
        make.height.mas_equalTo(MBHeightScale(50));
    }];
    
    [self addSubview:self.button4];
    [self.button4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(wid*3+MBWidthScale(26));
        make.centerY.equalTo(self);
        make.width.mas_equalTo(wid);
        make.height.mas_equalTo(MBHeightScale(50));
    }];
    
    [self addSubview:self.button5];
    [self.button5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(wid*4+MBWidthScale(42));
        make.centerY.equalTo(self);
        make.width.mas_equalTo(wid);
        make.height.mas_equalTo(MBHeightScale(50));
    }];
    
    [self addSubview:self.button6];
    [self.button6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(wid*5+MBWidthScale(47));
        make.centerY.equalTo(self);
        make.width.mas_equalTo(wid);
        make.height.mas_equalTo(MBHeightScale(50));
    }];
    
    //image
    [self addSubview:self.hmImageView];
    [self.hmImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.button2.mas_right).offset(MBWidthScale(6));
        make.centerY.equalTo(self);
        make.width.mas_equalTo(MBWidthScale(4));
        make.height.mas_equalTo(MBWidthScale(16));
    }];
    
    [self addSubview:self.msImageView];
    [self.msImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.button4.mas_right).offset(MBWidthScale(6));
        make.centerY.equalTo(self);
        make.width.mas_equalTo(MBWidthScale(4));
        make.height.mas_equalTo(MBWidthScale(16));
    }];
}

#pragma mark - Setter && Getter
- (UIButton *)button1 {
    if (!_button1) {
        _button1 = [UIButton new];
        _button1.userInteractionEnabled = NO;
        [_button1 setBackgroundImage:ImageWithName(@"home_nodata_time_back") forState:0];
        [_button1 setTitleColor:HEXS(@"B92176") forState:0];
        _button1.titleLabel.font = MBHelBoldFontSizeScale(20);
        [_button1 setTitle:@"0" forState:0];
    }
    return _button1;
}

- (UIButton *)button2 {
    if (!_button2) {
        _button2 = [UIButton new];
        _button2.userInteractionEnabled = NO;
        [_button2 setBackgroundImage:ImageWithName(@"home_nodata_time_back") forState:0];
        [_button2 setTitleColor:HEXS(@"B92176") forState:0];
        _button2.titleLabel.font = MBHelBoldFontSizeScale(20);
        [_button2 setTitle:@"0" forState:0];
    }
    return _button2;
}

- (UIButton *)button3 {
    if (!_button3) {
        _button3 = [UIButton new];
        _button3.userInteractionEnabled = NO;
        [_button3 setBackgroundImage:ImageWithName(@"home_nodata_time_back") forState:0];
        [_button3 setTitleColor:HEXS(@"B92176") forState:0];
        _button3.titleLabel.font = MBHelBoldFontSizeScale(20);
        [_button3 setTitle:@"0" forState:0];
    }
    return _button3;
}

- (UIButton *)button4 {
    if (!_button4) {
        _button4 = [UIButton new];
        _button4.userInteractionEnabled = NO;
        [_button4 setBackgroundImage:ImageWithName(@"home_nodata_time_back") forState:0];
        [_button4 setTitleColor:HEXS(@"B92176") forState:0];
        _button4.titleLabel.font = MBHelBoldFontSizeScale(20);
        [_button4 setTitle:@"0" forState:0];
    }
    return _button4;
}

- (UIButton *)button5 {
    if (!_button5) {
        _button5 = [UIButton new];
        _button5.userInteractionEnabled = NO;
        [_button5 setBackgroundImage:ImageWithName(@"home_nodata_time_back") forState:0];
        [_button5 setTitleColor:HEXS(@"B92176") forState:0];
        _button5.titleLabel.font = MBHelBoldFontSizeScale(20);
        [_button5 setTitle:@"0" forState:0];
    }
    return _button5;
}

- (UIButton *)button6 {
    if (!_button6) {
        _button6 = [UIButton new];
        _button6.userInteractionEnabled = NO;
        [_button6 setBackgroundImage:ImageWithName(@"home_nodata_time_back") forState:0];
        [_button6 setTitleColor:HEXS(@"B92176") forState:0];
        _button6.titleLabel.font = MBHelBoldFontSizeScale(20);
        [_button6 setTitle:@"0" forState:0];
    }
    return _button6;
}

- (UIImageView *)hmImageView {
    if (!_hmImageView) {
        _hmImageView = [UIImageView new];
        _hmImageView.image = ImageWithName(@"home_nodata_time_point");
    }
    return _hmImageView;
}

- (UIImageView *)msImageView {
    if (!_msImageView) {
        _msImageView = [UIImageView new];
        _msImageView.image = ImageWithName(@"home_nodata_time_point");
    }
    return _msImageView;
}
@end
